package view.clock_panel;

import java.awt.event.ActionEvent;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import listener.ParticipantChangeEvent;
import listener.ReachedZeroEvent;
import listener.ReachedZeroListener;

import clock.SecondTimer;

public class CounterButton extends BasicButton implements ListSelectionListener, ReachedZeroListener {
	private static final long serialVersionUID = -4198046377820386846L;
	private SecondTimer timer;
	
	public CounterButton(SecondTimer timer) {
		this.timer = timer;
		setText("Add participant!");
	}

	public void actionPerformed(ActionEvent e) {
		if (timer.isPaused()) {
			timer.start();
			setText("Stop");
		} else {
			timer.pause();
			setText("Start");
		}
	}

	public void valueChanged(ListSelectionEvent e) {
		timer.pause();
		setText("Start");
	}

	public void reachedZero(ReachedZeroEvent e) {
		timer.pause();
		setText("Time is up!");
	}

	public void participantChange(ParticipantChangeEvent e) {
		setEnabled(!e.getSource().getClock().timeIsUp());
		setText(isEnabled() ? "Start" : "Time is up!");
	}
}
